<?php
namespace app\api\service\company;
use app\shop\model\company\EntryFile;
use app\shop\model\company\EntryFileType;
use app\shop\model\company\Staff as StaffModel;
use app\shop\model\company\StaffLeave;
use app\shop\model\settings\Region;
use app\shop\model\user\User;
use app\common\service\Result;
use app\shop\validate\company\Staff as StaffValidate;
class Staff
{    //信息编辑
    public function editSelf($data)
    {
        try {
            $app_id = request()->app_id;
            $user_id = request()->user_id;
            $user = User::where('app_id', $app_id)->find($user_id);
            if (!$user) {
                return Result::error('用户不存在');
            }
            //是否已存在
            $staff = StaffModel::where('app_id', $app_id)->find($data['user_id']);
            if (!$staff) {
                return Result::error('你不是员工');
            }
            if (isset($data['real_name'])) {
                $user->real_name = $data['real_name'];
            }
            if (isset($data['mobile'])) {
                $user->mobile = $data['mobile'];
            }
            if (isset($data['age'])) {
                $user->age = $data['age'];
            }
            $native_place = $data['native_place'] ?? [];
            if ($native_place) {
                if (!is_array($native_place)) {
                    $native_place = explode(',', $native_place);
                }
                $province = $native_place[0] ?? 0;
                $city = $native_place[1] ?? 0;
                $user->province = $province;
                $user->city = $city;
            }
            $user->save();
            $staff->save($data);
            return Result::success();
        } catch (\Exception $e) {
            return Result::error($e->getMessage());
        }
    }
}